package com.xiaoyg;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * @author xiaoyg
 * @Title: Consumer
 * @ProjectName rabbitmq-headers
 * @Description: TODO
 * @date 2018/12/511:02
 */
public class Consumer {

    private final static String EXCHANGE_NAME = "header-exchange";
    private final static String QUEUE_NAME = "header-queue";

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.8.129");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("123456");
        connectionFactory.setPort(AMQP.PROTOCOL.PORT);

        Connection connection = connectionFactory.newConnection();

        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "headers",false,true,null);
        channel.queueDeclare(QUEUE_NAME,false,false,true,null);

        Map<String,Object> header = new Hashtable<>();
        header.put("x-match","any");
        header.put("bbb","01234");
        header.put("aaa","56789");


        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"",header);

        QueueingConsumer consumer = new QueueingConsumer(channel);
        // 指定接收者，第二个参数为自动应答，无需手动应答
        channel.basicConsume(QUEUE_NAME, true, consumer);
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(message);
        }


    }

}
